<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>

	<!-- This code written by : Willie Wheeler and John Wheeler -->
	<!-- For 'Spring In Practice' [Book written by Willie Wheeler and John Wheeler] -->

<c:url var="stylesUrl" value="/styles.css"/>

<html>
	<head>
		<title>Discussion Forums</title>
		<link rel="stylesheet" type="text/css" href="${stylesUrl}">
	</head>
	<body>
		<jsp:include page="header.jsp"/>
		
		<div id="body">
			<h1>Discussion Forums</h1>
			
			<c:choose>
				<c:when test="${empty forumList}">
					<p>There aren't any forums to view.</p>
				</c:when>
				<c:otherwise>
					<table style="width:600px">
						<tr>
							<th>Forum</th>
							<th>Messages</th>
							<th>Last Post</th>
							<security:authorize ifAnyGranted="ROLE_ADMIN">
								<th></th>
							</security:authorize>
						</tr>
						<c:forEach var="forum" items="${forumList}">
							<tr>
								<td><a href="forum.do?id=${forum.id}">${forum.name}</a></td>
								<td>${fn:length(forum.messages)}</td>
								<td>
									<c:choose>
										<c:when test="${empty forum.messages}">
											-
										</c:when>
										<c:otherwise>
											<fmt:formatDate value="${forum.lastMessageDate}"/>
										</c:otherwise>
									</c:choose>
								</td>
								<security:authorize ifAnyGranted="ROLE_ADMIN">
									<td><a href="#">Admin</a></td>
								</security:authorize>
							</tr>
						</c:forEach>
					</table>
				</c:otherwise>
			</c:choose>
		</div>
	</body>
</html>
